package com.qd.panda.func;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.constant.ConstantDto;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.panda.domain.entity.func.ChannelDTO;
import com.qd.common.panda.domain.entity.func.ChannelEnableDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.panda.service.func.ChannelOptionService;
import com.qd.panda.service.func.ChannelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 渠道/WEB_渠道推广
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/channel/")
@Slf4j
public class ChannelController {
    private final ChannelService channelService;
    private final ChannelOptionService channelOptionService;

    /**
     * 添加
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:channel:save')")
    @OperatorInfo(title = "添加", operator = OperatorType.INSERT)
    @PostMapping(value = "/save")
    public ChannelDTO save(@Validated({DefaultGroup.class}) @RequestBody ChannelDTO dto) {
        if (!StringUtils.hasLength(dto.getEnable())) {
            dto.setEnable(ConstantDto.SF1);
        }
        dto.setAddTime(LocalDateTime.now());
        return channelService.save(dto);
    }

    /**
     * 修改
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:channel:edit')")
    @OperatorInfo(title = "修改", operator = OperatorType.UPDATE)
    @PutMapping(value = "/update")
    public ChannelDTO update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody ChannelDTO dto) {
        dto.setUpdateTime(LocalDateTime.now());
        return channelService.update(dto);
    }

    /**
     * 删除
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:channel:delete')")
    @OperatorInfo(title = "删除", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        channelOptionService.delete(delDTO.getIds());
        return ApiResult.success("删除成功");
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public ChannelDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return channelService.getByIdDto(id);
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<ChannelDTO> page(ChannelDTO dto) {
        return channelService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 全部
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/list")
    public List<ChannelDTO> list(ChannelDTO dto) {
        dto = Optional.ofNullable(dto).orElse(new ChannelDTO());
        return channelService.getListDto(dto);
    }

    /**
     * 启用禁用
     *
     * @param dto 参数
     * @return 成功
     */
    @PreAuthorize("@ps.hasPermission('system:channel:enable')")
    @OperatorInfo(title = "启用禁用", operator = OperatorType.OTHER)
    @PutMapping(value = "/enable")
    public ApiResultWrapper<Object> enable(@Validated @RequestBody ChannelEnableDTO dto) {
        channelOptionService.enable(dto);
        dto.freeData();
        return ApiResult.success();
    }
}